package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.config.Config;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.AdminDao;
import com.yc.jpyy.mobile.service.AdminService;

@Controller
@RequestMapping(value = "/admin")
public class AdminCourseRecord extends BaseController {
	
	@Autowired
	private AdminService adminService;

	@Autowired
	private AdminDao adminDao;
	private static Logger logger = LoggerFactory.getLogger(AdminCourseRecord.class);
	
	/**
	 * 历史课程记录
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getHistoryCourseRecord", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getHistoryCourseRecord(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 驾校Id
			String driverId = request.getParameter("driverId");
			// 科目
			String subjectId = request.getParameter("subjectId");
			// 教练ID
			String teacherId = request.getParameter("teacherId");
			// 余量
			String count = request.getParameter("count");
			// 开始时间
			String beginTime = request.getParameter("beginTime");
			// 结束时间
			String endTime = request.getParameter("endTime");
			if(driverId != null && !driverId.isEmpty()
					&& beginTime != null && !beginTime.isEmpty()
					&& endTime != null && !endTime.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("driverId", driverId);
				paramsMap.put("subjectId", subjectId);
				paramsMap.put("teacherId", teacherId);
				paramsMap.put("count", count);
				paramsMap.put("beginTime", beginTime);
				paramsMap.put("endTime", endTime);
				paramsMap.put("imgUrl", Config.getImageAddress());
				response = adminService.getHistoryCourseRecord(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填字段！");
			}
		} catch (Exception e) {
			logger.info("getHistoryCourseRecord:"+ e.toString());
			response.setReturnMsg(0,"获取历史课程记录失败！");
		} finally {
			return response.toJSON();
		}
	}

	
	/**
	 * 查询可预约学员记录
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getAppointmentStudents", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getAppointmentStudents(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 总驾校Id
			String driverId = request.getParameter("driverId");
			// 课程时间
			String courseDate = request.getParameter("courseDate");
			// 课程时间段
			String courseTimeSlot = request.getParameter("courseTimeSlot");
			//课程学驾类型
			String driverType = request.getParameter("driverType");
			//名字或手机号
			String nameOrMobilePhone = request.getParameter("nameOrMobilePhone");
			// 课程预约范围
			String appRange = request.getParameter("appRange");
			// 分驾校ID
			String branchSchoolId = request.getParameter("branchSchoolId");
			// 班型IDS
			String classIds = request.getParameter("classIds");
			// 教练ID
			String teacherId = request.getParameter("teacherId");
			// 科目类型
			String courseType = request.getParameter("courseType");
			if(driverId != null && !driverId.isEmpty()
					&& courseDate != null && !courseDate.isEmpty()
					&& courseTimeSlot != null && !courseTimeSlot.isEmpty()
					&& driverType != null && !driverType.isEmpty()
					&& appRange != null && !appRange.isEmpty()
					&& branchSchoolId != null && !branchSchoolId.isEmpty()
					&& classIds != null && !classIds.isEmpty()
					&& teacherId != null && !teacherId.isEmpty()
					&& courseType != null && !courseType.isEmpty()){
				Map<String,String> paramsMap = new HashMap<>();
				paramsMap.put("driverId", driverId);
				paramsMap.put("appRange", appRange);
				paramsMap.put("courseDate", courseDate);
				paramsMap.put("courseTimeSlot", courseTimeSlot);
				paramsMap.put("driverType", driverType);
				paramsMap.put("nameOrMobilePhone", nameOrMobilePhone);
				paramsMap.put("appRange", appRange);
				paramsMap.put("branchSchoolId", branchSchoolId);
				paramsMap.put("classIds", classIds);
				paramsMap.put("teacherId", teacherId);
				paramsMap.put("courseType", courseType);
				paramsMap.put("imgUrl", Config.getImageAddress());
				response = adminService.getAppointmentStudents(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填字段！");
			}
		} catch (Exception e) {
			logger.info("getAppointmentStudents:"+ e.toString());
			response.setReturnMsg(1,"获取可约学员记录失败！");
		} finally {
			return response.toJSON();
		}
	}
	
	/**
	 * 管理端给学员预约课程
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/adminAddAppointment", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String adminAddAppointment(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		try {
			String stuId = request.getParameter("studentId");
			String teaId = request.getParameter("teacherId");
			String courseID = request.getParameter("courseID");
			String drivingType = request.getParameter("drivingType");
			String remark = request.getParameter("remark");
			if ((null != stuId && !stuId.isEmpty() && !stuId.equals("0"))
					&& (null != courseID && !courseID.isEmpty())
					&& (null != teaId && !teaId.isEmpty())) {
				paramsMap.put("stuId", stuId);
				paramsMap.put("teaId", teaId);
				paramsMap.put("Remark", remark);
				paramsMap.put("drivingType", drivingType);
				paramsMap.put("CourseID", courseID);
				response = adminService.courseAppoint(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("必填参数为空！");
			}
		} catch (Exception e) {
			logger.info("CourseAppointment" + e.toString());
			response.setErrorcode(1);
			response.setMessage("预约失败！");
		} finally {
			return response.toJSON();
		}	
	}
	
}
